<style scoped lang="scss">
.account-password-main {
  display: flex;
  flex-direction: column;
  width: 400px;
  .title-description {
    padding-top: 0;
    margin-top: 0;
    margin-bottom: 40px;
    font-size: 0.9rem;
  }
}
.other-login-icon {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  .icon {
    cursor: pointer;
    margin: 0 3px;
    color: rgb( 91, 94, 96);
    font-size: 30px;
    padding: 7px;
  }
  .icon:hover{
    background-color: rgb(244,244,245);
    color: black;
    border-radius: 50%;
  }
}
</style>

<template>
  <div class="account-password-main">
    <h1>欢迎回来 📲</h1>
    <p class="title-description">请输入您的手机号码以开始管理您的项目</p>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" size="medium">
      <el-form-item label="" prop="phone">
        <el-input v-model.number="loginForm.phone" v-waves prefix-icon="el-icon-mobile" clearable placheholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="" prop="code">
        <div class="" style="display: flex;flex-direction: row;justify-content: space-between;">
          <el-input v-model.trim="loginForm.code" v-waves type="text" prefix-icon="el-icon-postcard" clearable placeholder="请输入验证码" />
          <el-button v-waves lock text="验证码正在发送中" plain style="width: 300px;margin-left: 10px;" @click="clickSendCode">
            <span v-if="!codeLoading" style="color: rgb( 70,166,255)">{{ buttonText }}</span>
            <span v-else style="color: #999999;font-size: 0.9rem">{{ codeSendSecond }}秒后重新获取</span>
          </el-button>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button v-waves type="primary" style="width: 100%" @click="clickSubmitLogin">登录</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-waves type="" plain style="width: 100%" @click="$emit('switchLoginWay',1)">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'PhoneCodeLogin',
  data() {
    return {
      loginForm: {
        phone: '18083198680',
        code: ''
      },
      loginRules: {
        phone: [
          { required: true, trigger: 'blur', message: '请输入手机号' },
          { min: 11, max: 11, message: '请输入11位手机号码', trigger: 'blur' }
        ],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      buttonText: '获取验证码',
      codeSendSecond: 0, // 验证码等待接收的时长（秒）
      codeLoading: false
    }
  },
  created() {
  },
  methods: {
    clickSendCode() {
      // 发送验证码
      if (!this.loginForm.phone.length) {
        this.$message.error('请输入手机号')
        return
      }
      if (this.codeLoading) {
        this.$message.warning('验证码发送太频繁，请稍后')
        return
      }

      // 验证码倒计时
      this.codeLoading = true
      this.codeSendSecond = 60 // 60秒
      const s1 = setInterval(() => {
        this.codeSendSecond -= 1
      }, 1000)
      setTimeout(() => {
        this.codeLoading = false
        clearInterval(s1)
      }, 1000 * this.codeSendSecond)

      // 发送验证码 TODO...
      this.$message.success('发送成功')
    },
    clickSubmitLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/phoneLogin', this.loginForm)
            .then(() => {
              this.$router.push({ path: '/dashboard' })
              this.loading = false
              this.$notify({ title: '登录成功', message: `欢迎您回来！`, type: 'success', position: 'bottom-right' })
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

